<?php
namespace Home\Controller;
use Common\Controller\BaseController;

class HomeController extends BaseController {

    protected $is_weixin;

    public function _initialize(){
    	parent::_initialize();

        if (S($proMenu)) {
            $proMenu = S('proMenu');
        }else{
            $map['model'] = 2;
            $proMenu = M('Category')->field('id,name,path,fid as pid')->where($map)->order('sort asc,id asc')->select();
            $proMenu = array('data'=>$this->getTree($proMenu));
            S('proMenu',$proMenu);  
        }
        
        $this->assign('proMenu',$proMenu['data']);
        

        //新闻部分
        if (S('menu5')) {
            $menu5 = S('menu5');
        }else{
            $map['fid']=10;
            $menu5 = M('Category')->where($map)->order('sort asc,id asc')->select();
            S('menu5',$menu5);        
        }

        if (S('menu6')) {
            $menu6 = S('menu6');
        }else{
            $map['fid']=11;
            $menu6 = M('Category')->where($map)->order('sort asc,id asc')->select();
            S('menu6',$menu6);        
        }


        $this->assign('menu5',$menu5);
        $this->assign('menu6',$menu6);
   
    } 

    public function getTree($obj,$data='0',$level=1){
        foreach ($obj as $key=>$value){
            if($value['pid']==$data){
                $obj[$key]['childrens'] = $this->getTree($obj,$obj[$key]['id'],$level+1);
                $obj[$key]['leaf']=1;
                $obj[$key]['level']=$level;
                if($obj[$key]['childrens']==''){
                    $obj[$key]['childrens']=array();
                    $obj[$key]['leaf']=0;
                }
                $arr[] = $obj[$key];
            }
        }
        return $arr;
    }

    public function getTopNews($cid,$number){
        $map['cid'] = $cid; 
        $map['status'] = 1;
        $map['del'] = 0;      
        $list = M('Article')->field('id,title,picname,intr,createTime')->where($map)->order('sort asc,id desc')->limit($number)->select();
        foreach ($list as $key => $value) {
        	if ($value['url']!='') {
        		$list[$key]['link'] = $url;
        	}else{
        		$list[$key]['link'] = U('News/view',array('id'=>$value['id']));
        	}
        }
        return $list;
    }

    //获得access_token
    public function get_access_token(){
        if (S('AccessToken')) {
            return S('AccessToken');
        }else{
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('APP_ID').'&secret='.C('APP_SECRET');
            $con = file_get_contents($url);
            $con = json_decode($con);
            S('AccessToken',$con->access_token,3600);
            return S('AccessToken');
        }
    }


    //获得jsTicket
    public function get_jsapi_ticket(){
        if (S('JsTicket')) {
            return S('JsTicket');
        }else{
            $access_token = $this->get_access_token();
            $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
            $con = file_get_contents($url);
            $con = json_decode($con);
            $jsapi_ticket = $con->ticket;
            S('JsTicket',$jsapi_ticket,3600);
            return S('JsTicket');
        }       
    }  

    public function qrcode(){
        import('Common.ORG.Qrcode.phpqrcode');
        $value = I("data");//二维码数据
        $value = urldecode($value);
        $errorCorrectionLevel = 'Q';//纠错级别：L、M、Q、H
        $matrixPointSize = 10;//二维码点的大小：1到10
        \QRcode::png( $value, false, $errorCorrectionLevel, $matrixPointSize, 2);//不带Logo二维码的文件名
    }
}